//
//  YYENotesViewController.m
//  YuYingEDU
//
//  Created by 胡波 on 2017/12/27.
//  Copyright © 2017年 胡波. All rights reserved.
//

#import "YYENotesViewController.h"
#import "YYELatestNotesViewController.h"
#import "YYERecommandnotesViewController.h"
#import "SPPageMenu.h"
#import "YYESendNotesViewController.h"

@interface YYENotesViewController () <SPPageMenuDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) SPPageMenu *pageMenu;
@property (nonatomic,strong) UIScrollView *contentSV;

@end

@implementation YYENotesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = GeneralBackColor;
    
    [self configNavi];
    
    [self configUI];

}

- (void)configUI {
    
    NSArray *datas = @[@"最新",@"推荐"];
    
    self.pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, 180, 40) trackerStyle:SPPageMenuTrackerStyleLineAttachment];
    [self.pageMenu setWidth:90 forItemAtIndex:0];
    self.pageMenu.selectedItemTitleColor = [UIColor blackColor];
    self.pageMenu.unSelectedItemTitleColor = [UIColor blackColor];
//    self.pageMenu.tracker.image = [UIImage imageNamed:@"bottomLine"];
    self.pageMenu.tracker.backgroundColor = [UIColor blackColor];
    self.pageMenu.dividingLine.backgroundColor = [UIColor clearColor];
    self.pageMenu.needTextColorGradients = NO;
    self.pageMenu.delegate = self;
    self.pageMenu.backgroundColor = [UIColor clearColor];
//    self.pageMenu.bridgeScrollView.backgroundColor = [UIColor greenColor];
    self.pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollEqualWidths;
    self.navigationItem.titleView = self.pageMenu;
    
    self.contentSV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - 49 - SafeAreaBottomHeight)];
    self.contentSV.delegate = self;
    self.contentSV.pagingEnabled = YES;
    self.contentSV.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.contentSV];
    
    YYELatestNotesViewController *latestNoteVC = [[YYELatestNotesViewController alloc]init];
    YYERecommandnotesViewController *recommandVC = [[YYERecommandnotesViewController alloc]init];
    [self addChildViewController:latestNoteVC];
    [self addChildViewController:recommandVC];
 
    [self.contentSV addSubview:latestNoteVC.view];
    latestNoteVC.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - 49 - SafeAreaBottomHeight);
    self.contentSV.contentSize = CGSizeMake(SCREEN_WIDTH * 2, 0);
    
//    for (NSInteger i = 0; i < self.childViewControllers.count; i++) {
//
//        YYEBaseViewController *baseVC = self.childViewControllers[i];
//        [self.contentSV addSubview:baseVC.view];
//
//        baseVC.view.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaBottomHeight - 49);
//        self.contentSV.contentOffset = CGPointMake(SCREEN_WIDTH * i, 0);
//        self.contentSV.contentSize = CGSizeMake(self.childViewControllers.count * SCREEN_WIDTH, 0);
//
//    }
    
    self.pageMenu.bridgeScrollView = self.contentSV;
    [self.pageMenu setItems:datas selectedItemIndex:0];
    
}

- (void)configNavi {
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"feiji"] style:UIBarButtonItemStyleDone target:self action:@selector(planeClicked)];
    
}

#pragma mark -- SPPageMenuDelegate
- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index {
    
    self.contentSV.contentOffset = CGPointMake(SCREEN_WIDTH * index, 0);
    
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    
    if (labs(toIndex - fromIndex) >= 2) {
        [self.contentSV setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:NO];
    }else {
        [self.contentSV setContentOffset:CGPointMake(SCREEN_WIDTH * toIndex, 0) animated:YES];
    }
    
    UIViewController *targetViewController = self.childViewControllers[toIndex];
    // 如果已经加载过，就不再加载
    if ([targetViewController isViewLoaded]) return;
    
    targetViewController.view.frame = CGRectMake(SCREEN_WIDTH * toIndex, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - 49 - SafeAreaBottomHeight);
    [self.contentSV addSubview:targetViewController.view];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    
    UIViewController *targetViewController = self.childViewControllers[index];
    if ([targetViewController isViewLoaded]) return;
    
    self.contentSV.contentOffset = CGPointMake(SCREEN_WIDTH * index, 0);
    
    targetViewController.view.frame = CGRectMake(SCREEN_WIDTH * index, 0, SCREEN_WIDTH, SCREEN_HEIGHT - SafeAreaTopHeight - 49 - SafeAreaBottomHeight);
    [self.contentSV addSubview:targetViewController.view];
    
}

// 发布
- (void)planeClicked {
    
    YYESendNotesViewController *sendVC = [[YYESendNotesViewController alloc]init];
    [self.navigationController pushViewController:sendVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
